/*
 * 
 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * This software is the proprietary information of Sun Microsystems, Inc. Use is
 * subject to license terms.
 *  
 */

package cart;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import Bean.BeanBook;
import Bean.BeanClass;
import Bean.BeanStudent;

public class ShoppingCart {
    HashMap<Integer,ShoppingCartItem> items = null;

    int numberOfItems = 0;

    public ShoppingCart() {
        items = new HashMap<>();
    }

    public synchronized void add(int classId, BeanClass myclass, BeanStudent mystudent, int num) throws Exception {
        if (items.containsKey(myclass)) {
            throw new Exception("购物车中已存在该课程！");
        } else {
            ShoppingCartItem newItem = new ShoppingCartItem(myclass,mystudent,num);
            items.put(classId, newItem);
        }
        numberOfItems++;
    }

    public synchronized void remove(String bookId) {
        if (items.containsKey(bookId)) {
            items.remove(bookId);
            numberOfItems--;
        }
    }

    public synchronized Collection<ShoppingCartItem> getItems() {
        return items.values();
    }

    protected void finalize() throws Throwable {
        items.clear();
    }

    public synchronized int getNumberOfItems() {
        return numberOfItems;
    }

    public synchronized double getTotal() {
        double amount = 0.0;

        for (Iterator i = getItems().iterator(); i.hasNext();) {
            ShoppingCartItem item = (ShoppingCartItem) i.next();
            BeanClass classDetails = (BeanClass) item.getItem();
            amount += classDetails.getClass_price();
        }
        return roundOff(amount);
    }

    private double roundOff(double x) {
        long val = Math.round(x * 100); // cents
        return val / 100.0;
    }

    public synchronized void clear() {
        items.clear();
        numberOfItems = 0;
    }
}
